home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 415_02 / rtti / src / String.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-04  |  2.8 KB  |  131 lines

  1. #ifndef lint
  2. static const char sccsid[] = "%Z%%I%  %G% %U% %W%";
  3. #endif
  4. /*
  5.  * COMPONENT_NAME: (RTTI) Run-Time Type System for C++
  6.  *
  7.  * FUNCTIONS:
  8.  *
  9.  * ORIGINS: 27
  10.  *
  11.  * (C) COPYRIGHT International Business Machines Corp. 1992
  12.  * This work was supported by a grant from International Business
  13.  * Machines, Inc. These procedures are contributed to the public domain
  14.  * by International Business Machines Inc. "AS IS" without any warranty
  15.  * of any kind including the warranty of merchantability or fitness
  16.  * for a particular purpose.
  17.  *
  18.  *
  19.  * This is free software. Feel free to redistribute and/or modify it.
  20.  * Please send us e-mail and let us know if you have any problems
  21.  * or suggestions.
  22.  *
  23.  * Arindam Banerji
  24.  * axb@cse.nd.edu
  25.  */
  26.  
  27.  
  28.  
  29. // String.cc
  30. //
  31. // Implementation of string class  - based on Stousroup's
  32. // implementation.
  33.  
  34. #include  <string.h>
  35. #include  "String.h"
  36.  
  37. #ifndef   _KERNEL
  38. #include    <iostream.h>
  39. #else
  40. #include    <stdio.h>
  41. #endif  // _KERNEL
  42.  
  43. static   char    err_val = '\0' ;
  44.  
  45. string::string()
  46. {
  47.   p = new srep ;
  48.   p->s = 0 ;
  49. } // the trivial constructor
  50.  
  51. string::string ( const string &x )
  52. {
  53.  x.p->n++ ; p = x.p ;
  54. } // increment the ref. count constructor
  55.  
  56. string::string(const char *s )
  57. {
  58.   p = new srep ;
  59.   p->s = new char [strlen(s) + 1 ] ;
  60.   strcpy ( p->s, s ) ;
  61. } // copy into new string
  62.  
  63. string::operator char *()
  64. {
  65.  char *z = new char [strlen (p->s) + 1];
  66.  strcpy (z, p->s ) ;
  67.  return (z) ;
  68. } // get a pointer to the string
  69.  
  70. string::~string()
  71. {
  72.   if ( --p->n == 0 )
  73.    {
  74.      delete [] p->s ;
  75.      delete p ;
  76.    } // delete is necessary
  77. } // destructor - reduce ref. count
  78.  
  79. string& string::operator= (const char *s )
  80. {
  81.  if ( p->n > 1 )
  82.    {
  83.     p->n-- ; p = new srep ;
  84.    }  // disconnect self
  85.  else
  86.    delete [] p->s ;  // free the old string
  87.  
  88.  p->s = new char [strlen(s) + 1 ]  ;
  89.  strcpy ( p->s, s ) ;
  90.  return ( *this) ;
  91. } // assignment operator - handles cleanup.
  92.  
  93. string& string::operator= (const string &x)
  94. {
  95.   x.p->n++ ; // make sure that x = x situation does not arise
  96.   if (--p->n == 0 )
  97.     {
  98.       delete [] p->s ; delete p ;
  99.     } // remove old representation
  100.   p = x.p ; // set current rep. to point to that of the passed argument
  101.   return ( *this ) ;
  102. } // assignment operator - smiliar to the constructor + handles cleanup
  103.  
  104. #ifndef    _KERNEL
  105. ostream& operator<< (ostream &s, const string &x )
  106. {
  107.   return  (s << x.p->s << " [ " << x.p->n << " ] " ) ;
  108. } //  display the string
  109.  
  110. istream& operator>> (istream &s, string &x )
  111. {
  112.  char buf[INPUTBUFSIZE] ;
  113.  s >> buf ; // might overflow
  114.  x = buf ;
  115.  cout << "echo : " << x << '\n' ;
  116.  return s ;
  117. } // this is unsafe at present - so be careful
  118. #else
  119. void string::display()
  120. {
  121.  printf("%s\n", p->s) ;
  122. } // display the string
  123. #endif //    _KERNEL
  124.  
  125. char &string::operator[] ( int i )
  126. {
  127.   if ( i < 0 || strlen(p->s) < i )
  128.      return (err_val) ;
  129.   return ( p->s[i]) ;
  130. } // allows indexing into string array
  131.